package team.se.tms.controller.stage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import team.se.tms.annotation.CheckFormat;
import team.se.tms.annotation.Number;
import team.se.tms.annotation.Regex;
import team.se.tms.controller.BaseController;
import team.se.tms.dto.GroupDetail;
import team.se.tms.dto.Page;
import team.se.tms.dto.ProjectStageDetail;
import team.se.tms.enums.DateEnum;
import team.se.tms.exception.UpdateException;
import team.se.tms.po.LabelLibInfo;
import team.se.tms.po.TeachCourseInfo;
import team.se.tms.service.LabelLibInfoService;
import team.se.tms.service.StageInfoService;
import team.se.tms.service.TeachCourseInfoService;
import team.se.tms.util.ResultUtil;

import java.util.List;

@Controller
@RequestMapping("project_stage")
public class ProjectStageController extends BaseController {

    @Autowired
    StageInfoService stageInfoService;
    @Autowired
    TeachCourseInfoService teachCourseInfoService;
    @Autowired
    LabelLibInfoService labelLibInfoService;
    /**
     * 查看项目阶段详情
     *
     * @param param 参数描述
     * @return String 页面地址
     * @throws Exception 异常
     * @create on 2019/1/1 by johnny
     */
    @RequestMapping("view")
    public ModelAndView toViewProjectStageDetail(String projectId) throws Exception{
        ModelAndView mv = this.getModelAndView();
        ProjectStageDetail projectStageDetail = stageInfoService.getProjectStageDetail(projectId);
        mv.addObject("projectStageDetail",projectStageDetail);
        mv.setViewName("stage/poject_process_details");
        return mv;
    }
    /**
     * 项目流程判定
     *
     * @param projectId 项目id
     * @param stageId 阶段id
     * @param grade 分数
     * @return String 页面地址
     * @throws Exception 异常
     * @create on 2019/1/1 by johnny
     */

    @RequestMapping("edit")
    @ResponseBody
    //@CheckFormat
    public Object editProjectStage(@Regex("^[A-Za-z0-9]{16}$") String projectId,
                                   @Regex("^[A-Za-z0-9]{16}$") String stageId,
                                  float grade) throws Exception{
        boolean result  = stageInfoService.modifyProjectStageComplete(projectId,stageId,grade);
        if(!result){
            throw new UpdateException();
        }
        return ResultUtil.success();
    }
    /**
     * 去查看项目进度界面
     *
     * @return String 页面地址
     * @throws Exception 异常
     * @create on 2019/1/1 by johnny
     */
    @RequestMapping("list")
    public ModelAndView toSearchProjectProcess() throws Exception{
        ModelAndView mv = this.getModelAndView();
        //List<LabelLibInfo> labelLibInfoList=labelLibInfoService.listAllLabelLibInfo(null);
        List<TeachCourseInfo> teachCourseInfoList =teachCourseInfoService.listAllTeachCourseInfos(null);
        mv.addObject("teachCourseInfoList",teachCourseInfoList);
        mv.setViewName("stage/project_process_list");
        return mv;
    }
    /**
     * 根据标签库id查询授课信息
     *
     * @param labelLibId 标签库id
     * @return Object 授课信息列表
     * @throws Exception 异常
     * @create on 2019/1/3 by johnny
     */
    @RequestMapping("getTeachCourse")
    @ResponseBody
    public Object searchTeachCourses(String labelLibId) throws Exception{
        List<TeachCourseInfo> teachCourseInfoList
                = teachCourseInfoService.listTeachCourseInfosByLabelLibId(labelLibId);
        return teachCourseInfoList;
    }
    /**
     * 获取所有项目进度
     *
     * @param page 分页
     * @param teachCourseId 授课id
     * @return String 页面地址
     * @throws Exception 异常
     * @create on 2019/1/1 by johnny
     */
    @RequestMapping("getList")
    @ResponseBody
    public String searchProjectStageDetail(Page page,String teachCourseId) throws Exception{
        if("".equals(teachCourseId)){
            teachCourseId =null;
        }
        List<ProjectStageDetail> projectStageDetailList= stageInfoService.listAllProjectStageDetail(page,teachCourseId);
        return page.getDataTabelJson(projectStageDetailList, DateEnum.ACCURATE_TO_DAY);
    }

}
